<style scoped>
    @import '../../styles/tasks.css';
</style>

<template>
    <div class="layout-content-main">
        <Card :bordered="false" :dis-hover="true" class="form-style">
            <p slot="title">修改任务</p>
            <Form ref="editForm" :model="editForm" :rules="ruleValidate" :label-width="80">
                <FormItem label="任务名称" prop="JOB_NAME">
                    <Input v-model="editForm.JOB_NAME" placeholder="请输入任务名称"></Input>
                </FormItem>
                <FormItem label="任务组" prop="JOB_GROUP">
                    <Input v-model="editForm.JOB_GROUP" placeholder="请输入任务组"></Input>
                </FormItem>
                <FormItem label="cron" prop="CRON_EXPRESSION">
                    <Input v-model="editForm.CRON_EXPRESSION" placeholder="请输入cron"></Input>
                </FormItem>
                <FormItem label="说明" prop="DESCRIPTION">
                    <Input v-model="editForm.DESCRIPTION" type="textarea" :autosize="{minRows: 2,maxRows: 5}"
                           placeholder=""></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('editForm')">提交</Button>
                    <Button type="ghost" @click="handleReset('editForm')" style="margin-left: 8px">重置</Button>
                </FormItem>
            </Form>
        </Card>
    </div>
</template>
<script>
    import axios from 'axios';

    var http = axios.create({
        baseURL: 'http://localhost',
        headers: {
            post: {'Content-Type': 'application/json;charset=UFT-8'}
        }
    });
    export default {

        data() {
            return {
                query: this.$route.query,
                editForm: {
                    JOB_NAME: '',
                    JOB_GROUP: 'DEFAULT_JOB_GROUP',
                    CRON_EXPRESSION: '',
                    JOB_DATA: {
                        DEFAULT_JOB_NAME_KEY: '',
                        DEFAULT_JOB_SCRIPT_KEY: ''
                    },
                    DESCRIPTION: ''
                },
                ruleValidate: {
                    JOB_NAME: [
                        {required: true, message: '任务名不能为空', trigger: 'blur'}
                    ],
                    JOB_GROUP: [
                        {required: true, message: '任务组不能为空', trigger: 'blur'}
                    ],
                    CRON_EXPRESSION: [
                        {required: true, message: '表达式不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {

            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var data = {
                            name: this.editForm.JOB_NAME,
                            group: this.editForm.JOB_GROUP,
                            cron: this.editForm.CRON_EXPRESSION,
                            data: this.editForm.JOB_DATA,
                            scriptPath: this.editForm.JOB_DATA.DEFAULT_JOB_SCRIPT_KEY,
                            description: this.editForm.DESCRIPTION
                        }
                        var that = this;
                        http.post('/job/saveOrUpdate.do', data).catch(function (thrown) {
                            console.log('catch', thrown.message);
                            that.$Message.error(thrown.message);
                        }).then(function (response) {
                            if (response.data.code == '1') {
                                that.$Message.success("操作成功");
                                that.rightPanel("/tasks");
                            } else {
                                that.$Message.error(response.data.msg);
                            }
                        })

                    } else {
                        this.$Message.error('请修改表单后重新提交!');
                    }
                })
            },
            handleReset(name) {
                this.$refs[name].resetFields();
            },
            rightPanel(router_path) {
                this.$router.push(router_path)
            }
        },
        mounted() {
            var that = this;
            var name = this.query.name;
            var group = this.query.group;
            if (name == '') {//新增
                that.editForm = {
                    JOB_NAME: '',
                    JOB_GROUP: 'DEFAULT_JOB_GROUP',
                    CRON_EXPRESSION: '',
                    JOB_DATA: {
                        DEFAULT_JOB_NAME_KEY: '',
                        DEFAULT_JOB_SCRIPT_KEY: ''
                    },
                    DESCRIPTION: ''
                }
            } else {//修改
                http.get('/job/get.do?name=' + name + "&group=" + group).catch(function (thrown) {
                    console.log('catch', thrown.message);
                }).then(function (response) {
                    var pageResult = response.data.result;
                    that.editForm = pageResult;
                })
            }
        }
    }
</script>

